﻿/*  ------------------------------------------------------------------------
    NQ Common Services Library
    
    Homepage: http://www.awzhome.de/
    ------------------------------------------------------------------------
    
    The contents of this file are subject to the Mozilla Public License
    Version 1.1 (the "License"); you may not use this file except in
    compliance with the License. You may obtain a copy of the License at
    http://www.mozilla.org/MPL/
    
    Software distributed under the License is distributed on an "AS IS"
    basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
    License for the specific language governing rights and limitations
    under the License.
   
    The Original Code is code of NQ Common Services Library.

    The Initial Developer of the Original Code is Andreas Weizel.
    Portions created by the Initial Developer are
    Copyright (C) 2006-2009 Andreas Weizel. All Rights Reserved.
    
    Contributor(s): (none)
    
    Alternatively, the contents of this file may be used under the terms
    of the GNU General Public License Version 2.0 or later (the "GPL"),
    or the GNU Lesser General Public License Version 2.1 or later (the
    "LGPL"), in which case the provisions of the GPL or the LGPL are
    applicable instead of those above. If you wish to allow use of your
    version of this file only under the terms of the GPL or the LGPL and
    not to allow others to use your version of this file under the MPL,
    indicate your decision by deleting the provisions above and replace
    them with the notice and other provisions required by the GPL or the
    LGPL. If you do not delete the provisions above, a recipient may use
    your version of this file under the terms of any of the MPL, the GPL
    or the LGPL.
    ------------------------------------------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AWZhome.NQ.Core;
using AWZhome.NQ.Core.Fluent;
using AWZhome.NQ.CommonServices.Visual;
using System.Windows;

namespace AWZhome.NQ.CommonServices
{
	[NQExportedService(Category = NQHostMode.GUI)]
	public class UpdateProgressView : ViewServiceBase, Services.IUpdateProgressViewService
	{
		private UpdateProgressViewVisual _viewVisual;
		private UpdateProgressInfo _progressInfo;
		private IUpdateManager _servUpdate;
		private IResourceManager _servRes;

		public UpdateProgressView(ISession sessionService)
			: base(sessionService)
		{
		}

		#region UpdateProgressInfo class

		public class UpdateProgressInfo : DependencyObject
		{
			public static readonly DependencyProperty ProgressProperty =
					DependencyProperty.Register("Progress", typeof(int), typeof(UpdateProgressInfo), new UIPropertyMetadata(0));
			public static readonly DependencyProperty ProgressVisibleProperty =
					DependencyProperty.Register("ProgressVisible", typeof(bool), typeof(UpdateProgressInfo), new UIPropertyMetadata(true));
			public static readonly DependencyProperty ActionTextProperty =
					DependencyProperty.Register("ActionText", typeof(string), typeof(UpdateProgressInfo), new UIPropertyMetadata(String.Empty));
			public static readonly DependencyProperty ProgressTextProperty =
					DependencyProperty.Register("ProgressText", typeof(string), typeof(UpdateProgressInfo), new UIPropertyMetadata(String.Empty));

			public int Progress
			{
				get
				{
					return (int) GetValue(ProgressProperty);
				}
				set
				{
					SetValue(ProgressProperty, value);
				}
			}

			public bool ProgressVisible
			{
				get
				{
					return (bool) GetValue(ProgressVisibleProperty);
				}
				set
				{
					SetValue(ProgressVisibleProperty, value);
				}
			}

			public string ActionText
			{
				get
				{
					return (string) GetValue(ActionTextProperty);
				}
				set
				{
					SetValue(ActionTextProperty, value);
				}
			}

			public string ProgressText
			{
				get
				{
					return (string) GetValue(ProgressTextProperty);
				}
				set
				{
					SetValue(ProgressTextProperty, value);
				}
			}
		}

		#endregion

		#region UpdateSelectView methods and properties

		public WizardDialogService WizardDialogService
		{
			get
			{
				return this.ViewManager as WizardDialogService;
			}
		}

		public UpdateProgressInfo ProgressInfo
		{
			get
			{
				return _progressInfo;
			}
			set
			{
				_progressInfo = value;
			}
		}

		#endregion

		#region NQViewServiceBase methods and properties

		protected override bool CheckSessionType(ISession session)
		{
			return true;
		}

		public override string DisplayName
		{
			get
			{
				return String.Empty;
			}
		}

		public override bool IsChanged
		{
			get
			{
				return false;
			}
		}

		public override bool CanBeClosed
		{
			get
			{
				return false;
			}
		}

		public override bool IsLoggable
		{
			get
			{
				return false;
			}
		}

		public override bool IsDocument
		{
			get
			{
				return false;
			}
		}

		public override object DisplayedObject
		{
			get
			{
				return _viewVisual;
			}
		}

		public override System.Windows.Media.ImageSource Icon
		{
			get
			{
				return null;
			}
		}

		public override ViewDisplayMode DockingType
		{
			get
			{
				return ViewDisplayMode.InClientArea;
			}
		}

		public override void Initialize(string context)
		{
			base.Initialize(context);

			// Initialize visual view
			_viewVisual = new UpdateProgressViewVisual();

			// Create update progress info object
			_progressInfo = new UpdateProgressInfo();
			_viewVisual.DataContext = _progressInfo;

			// Get service instances
			_servRes = ServiceCore
				.Get<Services.IResourceService>()
				.To;
			_servUpdate = ServiceCore
				.Get<Services.IUpdateService>()
				.To;

			// Set event handlers
			_servUpdate.UpdateProgress += _servUpdate_UpdateProgress;
			_servUpdate.UpdateError += _servUpdate_UpdateError;
		}

		#endregion

		#region Other private methods

		private void _servUpdate_UpdateProgress(object sender, UpdateProgressEventArgs e)
		{
			if (this.ViewManager.ActiveView == this)
			{
				if (_servUpdate.CurrentOperation == UpdateOperationType.LoadingUpdateInformation)
				{
					_progressInfo.ActionText = _servRes.GetResString("NQCS", "UpdateDialog.Label.DownloadUpdateInfo");
				}
				else if (_servUpdate.CurrentOperation == UpdateOperationType.LoadingUpdatedFiles)
				{
					_progressInfo.ActionText = _servRes.GetResString("NQCS", "UpdateDialog.Label.DownloadUpdates");
				}
				_progressInfo.ProgressVisible = true;

				// Update progress display
				switch (e.ProgressType)
				{
					case UpdateProgressEventType.Connecting:
						_progressInfo.ProgressText = _servRes.GetResString("NQCS", "UpdateDialog.Label.ProcessConnecting", e.ObjectName);
						_progressInfo.Progress = 0;
						break;

					case UpdateProgressEventType.Decompressing:
						_progressInfo.ProgressText = _servRes.GetResString("NQCS", "UpdateDialog.Label.ProcessDecompressing", e.ObjectName);
						_progressInfo.Progress = 0;
						break;

					case UpdateProgressEventType.Downloading:
						_progressInfo.ProgressText = _servRes.GetResString("NQCS", "UpdateDialog.Label.ProcessDownload", e.ObjectName, e.Progress.ToString());
						_progressInfo.Progress = e.Progress;
						break;

					case UpdateProgressEventType.Installing:
						_progressInfo.ProgressText = _servRes.GetResString("NQCS", "UpdateDialog.Label.ProcessInstalling", e.ObjectName, e.Progress.ToString());
						_progressInfo.Progress = e.Progress;
						break;

					case UpdateProgressEventType.Ready:
						_progressInfo.ProgressText = _servRes.GetResString("NQCS", "UpdateDialog.Label.ProcessReady");
						_progressInfo.Progress = 100;
						_progressInfo.ProgressVisible = false;
						break;

					//case NQUpdateProgressEventType.Aborted:
					//  _progressInfo.ProgressText = _servRes.GetResString("NQCS", "UpdateDialog.Label.ProcessError");
					//  _progressInfo.Progress = 0;
					//  _progressInfo.ProgressVisible = false;
					//  break;

					default:
						break;
				}
			}
		}

		private void _servUpdate_UpdateError(object sender, UpdateErrorEventArgs e)
		{
			if (this.ViewManager.ActiveView == this)
			{
				// Show an error message box, when this is the active view
				MessageBox.Show(_servRes.GetResString("NQCS", "AppErr.UpdateError", e.Message),
					_servRes.GetResString("NQCS", "UpdateDialog.Title"),
					MessageBoxButton.OK, MessageBoxImage.Exclamation);

				// Clean user interface
				_progressInfo.Progress = 0;
				_progressInfo.ProgressVisible = false;
				_progressInfo.ProgressText = _servRes.GetResString("NQCS", "UpdateDialog.Label.ProcessError");
			}
		}

		#endregion
	}
}
